package net.zjitc.controller;

import net.zjitc.common.ResponseResult;
import net.zjitc.entity.Manager;
import net.zjitc.exception.PermissionException;
import net.zjitc.service.ManagerService;
import net.zjitc.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

/**
 * @author 毛钰深
 * @create 2022/1/27
 */

@RestController
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private ManagerService managerService;


    /**
     * 所有权限列表
     * @param type
     * @return
     */
    @PreAuthorize("hasAuthority('147')")
    @GetMapping("/rights/{type}")
    public ResponseResult selectRights(@PathVariable String type) {
        ResponseResult res = new ResponseResult();
        try {
            res = permissionService.selectPermission(type);
        } catch (PermissionException e) {
            res.setError(e.getMessage());
        }
        return res;
    }

    /**
     * 左侧菜单权限
     * @return
     */
    @GetMapping("/menus")
    public ResponseResult selectLeftRights(Principal principal) {
        Manager manager = managerService.getByUsername(principal.getName());
        return permissionService.selectLeftPermission(manager.getRid());
    }
}
